package liuyang.bigeventserver.modules.upload.controller;

import cn.hutool.http.body.MultipartBody;
import liuyang.bigeventserver.common.Id;
import liuyang.bigeventserver.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 *
 * @author xconf
 * @since 2024/3/3
 */
@RestController
@RequestMapping("upload")
@Slf4j
public class FileUploadController {

    private static final String LOCAL_FILE_PATH = "D:\\workspaces_test\\big_event\\";

    @PostMapping("/upload01")
    public R upload(MultipartFile file) throws IOException {
        //String fileName = file.getOriginalFilename() + "_" + System.currentTimeMillis();
        //String fileName = file.getOriginalFilename() + "_" + Id.nextId() + "_" + ".jpg";
        //String originalFilename = file.getOriginalFilename();
        //String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = Id.nextId()+ "_" + file.getOriginalFilename();
        File destination = new File(LOCAL_FILE_PATH + fileName);
        file.transferTo(destination);
        return R.ok("文件上传成功").put("path", destination.getAbsolutePath()).put("fileName", fileName);
    }
}
